/**
 * Created by smp on 2017/3/21.
 */
var modelPath=ROOT_PATH+'/permission/dataAuthority/';
/**
 * 关闭layer
 */
function closeLayer() {
    layer.close(index);
}
/**
 * 使用layer 打开一个页面
 * @param title
 * @param content
 */
function openLayer(title,content) {

    index=layer.open({
        type: 2,
        title: title,
        shadeClose: true,
        area: ['600px', '600px'],
        content: modelPath+content
    });
}
function result(data) {
    if(data==1){
        layer.msg("保存成功");
        closeLayer();
    }else{
        layer.alert("保存失败");
    }
}
$(function () {
    var DEPTIdIFNULL=null;
    //初始化组织结构树
    $('#department_tree').tree({
        url: ROOT_PATH + '/permission/department/getDepartmentTree.shtml?parentId=0',
        lines:true,
        onSelect: function (node) {
            $('#tb1').form('reset');
            //重新按部门加载datagrid
            $('#dg1').datagrid('load', {
                deptId: node.id
            });
        },
        onLoadSuccess: function (node, data) {
            var deptId = data[0].id;
            DEPTIdIFNULL=deptId;
            //初始化加载datagrid
            $('#dg1').datagrid({
                method: 'post',
                url: ROOT_PATH + '/permission/dataAuthority/list.shtml',
                queryParams: {
                    deptId: deptId
                },
                toolbar:'#tb1',
                //idField: 'id',
                fitColumns: true,
                pagination: true,
                rownumbers: true,
                sortOrder: true,
                striped: true,
                singleSelect:true,
                fit: true,
                columns: [[
                    {field: 'id', width: 80, checkbox: true},
                    {field: 'code', title: '角色标识', width: 100,  sortable: true},
                    {field: 'name', title: '角色名称', width: 80,  sortable: true},
                    {
                        field: 'deptId',
                        title: '角色部门id',
                        width: 80,
                        sortable: true,
                        formatter: function (value, row, index) {
                            if (row.userRole&&row.userRole.deptId) {
                                return row.userRole.deptId;
                            } else {
                                return '';
                            }
                        },
                        hidden: true
                    },
                    {
                        field: 'groupName',
                        title: '角色组',
                        width: 80,
                        sortable: false,
                        formatter: function (value, row, index) {
                            if (row.roleGroup&&row.roleGroup.name) {
                                return row.roleGroup.name;
                            } else {
                                return '';
                            }
                        }
                    },
                    {field: 'sort', title: '排序号', width: 100, sortable: true,hidden: true},
                    {field: 'remarks', title: '备注', width: 100}
                ]]
            });
        }
    });
    $('#btn_clear').on("click", function () {
        $('#tb1').form('reset');
    });
    $('#btn_search').on("click", function () {
        var getSelected = $("#department_tree").tree('getSelected');
        var roleName = $(':input[name=roleName]').val();
        var deptId=DEPTIdIFNULL;
        if(getSelected){
            deptId=getSelected.id
        }
        $('#dg1').datagrid('load', {deptId:deptId,roleName:roleName});
    });
    $("#btn_dataAuthority_edit").on("click",function(){
        var checkedRows=$('#dg1').datagrid('getChecked');

        if(checkedRows.length == 0){
            layer.alert("选择需要授权的行！");
            return
        }else if (checkedRows.length > 1){
            layer.alert("只能选中一行数据！");
            return
        }
        var roleId=checkedRows[0].id;
        var deptId=checkedRows[0].userRole.deptId;
        var title='授予角色数据权限';
        var content='editDataAuthority.shtml?roleId='+roleId+'&deptId='+deptId;
        openLayer(title,content);
    });
});